package cn.didi.feidi.javabackend.server.user.epository;

import cn.didi.feidi.javabackend.mysql.jpa.entity.Userxx;
import cn.didi.feidi.javabackend.mysql.jpa.user.User;
import cn.didi.feidi.javabackend.server.user.constant.MysqlJpaHttpAddress;
import cn.didi.feidi.javabackend.server.user.constant.RedisHttpAddress;
import org.springframework.stereotype.Repository;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/10.
 */
@Repository
//@ComponentScan(basePackages = {"cn.didi.feidi.javabackend.server.user.constant"})
public class UserRep {

//   @Autowired
    RestTemplate restTemplate = new RestTemplate();

    public User getUserById(String id){
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        String path = "/getUserById";
        User user = restTemplate.getForObject(getServerURL(RedisHttpAddress.redisHost, RedisHttpAddress.redisPort, path, params), User.class);
        if(user == null){
            user = restTemplate.getForObject(getServerURL(MysqlJpaHttpAddress.mysqlJpaHost, MysqlJpaHttpAddress.mysqlJpaPort, path, params),User.class);
            if(user != null){
                restTemplate.postForObject(getServerURL(RedisHttpAddress.redisHost,  RedisHttpAddress.redisPort, "/addUser", null), user, String.class);
            }
            else{
                System.out.println("该user不存在");
            }
        }
        return user;
    }

    public void addUser(User user){
        String path = "/" + Thread.currentThread().getStackTrace()[1].getMethodName();
        restTemplate.postForObject(getServerURL(RedisHttpAddress.redisHost,  RedisHttpAddress.redisPort, path, null), user, String.class);
        restTemplate.postForObject(getServerURL(MysqlJpaHttpAddress.mysqlJpaHost, MysqlJpaHttpAddress.mysqlJpaPort, path, null), user, String.class);
    }

    public void updateUser(User user){
        String path = "/" + Thread.currentThread().getStackTrace()[1].getMethodName();
        restTemplate.postForObject(getServerURL(RedisHttpAddress.redisHost,  RedisHttpAddress.redisPort, path, null), user, String.class);
        restTemplate.postForObject(getServerURL(MysqlJpaHttpAddress.mysqlJpaHost, MysqlJpaHttpAddress.mysqlJpaPort, path, null), user, String.class);
    }

    public void updateUserById(String id, Map<String, Object> params){
        String path = "/" + Thread.currentThread().getStackTrace()[1].getMethodName();
        params.put("id", id);
        restTemplate.postForObject(getServerURL(MysqlJpaHttpAddress.mysqlJpaHost, MysqlJpaHttpAddress.mysqlJpaPort, path, null), params, void.class);
    }

    public void deleteUserById(String id){
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        String path = "/" + Thread.currentThread().getStackTrace()[1].getMethodName();
        restTemplate.delete(getServerURL(RedisHttpAddress.redisHost,  RedisHttpAddress.redisPort, path, params));
        restTemplate.delete(getServerURL(MysqlJpaHttpAddress.mysqlJpaHost, MysqlJpaHttpAddress.mysqlJpaPort, path, params));
    }

    public List<User> getUsersByName(String name){
        String path = "/" + Thread.currentThread().getStackTrace()[1].getMethodName();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("name", name);
        List<User> users = restTemplate.getForObject(getServerURL(MysqlJpaHttpAddress.mysqlJpaHost, MysqlJpaHttpAddress.mysqlJpaPort, path, params), List.class);
        return users;
    }

    public void addUserxx(Userxx userxx){
//        restTemplate.put(getServerURL(RedisHttpAddress.redisHost, RedisHttpAddress.redisPort, null), userxx);
        String path = "/addUserxx";

//        HttpHeaders headers = new HttpHeaders();
//        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
//        headers.setContentType(type);
//        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
//
//        JSONObject jsonObj = JSONObject.fromObject(userxx);
//
//        HttpEntity<String> formEntity = new HttpEntity<String>(jsonObj.toString(), headers);
//
//        MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
//        map.add("name", "xx");
//        map.add("password", "xx");
//
//        HttpEntity<MultiValueMap<String,String>> entity =
//                new HttpEntity<MultiValueMap<String, String>>(map, headers);

        String result = restTemplate.postForObject(getServerURL(MysqlJpaHttpAddress.mysqlJpaHost, MysqlJpaHttpAddress.mysqlJpaPort, path, null), userxx, String.class);
        restTemplate.postForObject(getServerURL(RedisHttpAddress.redisHost,  RedisHttpAddress.redisPort, path, null), userxx, String.class);
    }

    private String resolveVariables(Map<String, Object> params){
        if(params == null)
            return "";
        StringBuilder sb = new StringBuilder("?");
        Iterator<Map.Entry<String, Object>> iterator = params.entrySet().iterator();
        while(iterator.hasNext()){
            Map.Entry<String, Object> entry = iterator.next();
            sb.append(entry.getKey()+"="+entry.getValue()+"&");
        }
        int length = sb.toString().length();
        return sb.toString().substring(0, length - 1);
    }

    private String getServerURL(String host, String port, String path, Map<String, Object> params){
        String result = "http://" + host + ":" + port + path + resolveVariables(params);
        System.out.println(result);
        return result;
    }


}
